package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

	public class AddRemoveStored extends JPanel{
		
	private static final long serialVersionUID = -3607669981084633127L;
	private Manager manager;
	//private JFrame main;
	private JTextField upcText;
	private JButton reItem, addStored;
	private JTextArea textArea;
	private JScrollPane confirmation;
	private JTextField nameText;
	private JTextField stockText;
	
	public AddRemoveStored(Manager manager){
		this.manager = manager;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		addPanel();
		confirmPanel();
		
	}
	
	private void addPanel(){
		JPanel adder = new JPanel();
		adder.setLayout(new GridLayout(5, 2));
		adder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		adder.add(new JLabel("Name of Store:"));
		nameText = new JTextField();
		nameText.setPreferredSize(new Dimension(300, 30));
		adder.add(nameText);
		
		adder.add(new JLabel("UPC:"));
		upcText = new JTextField();
		upcText.setPreferredSize(new Dimension(300, 30));
		adder.add(upcText);
		
		adder.add(new JLabel("Stock of Item(Not required for removing):"));
		stockText = new JTextField();
		stockText.setPreferredSize(new Dimension(300, 30));
		adder.add(stockText);
		
		adder.add(new JLabel("Click to add Stored"));
		addStored = new JButton("add");
		addStored.addActionListener(new addStored());
		adder.add(addStored);
		
		adder.add(new JLabel("Click to remove item"));
		reItem = new JButton("remove Item");
		reItem.addActionListener(new removeItem());
		adder.add(reItem);
		
		add(adder);
	}
	
	private void confirmPanel(){
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Confirmation");
		conPanel.setBorder(title);
		
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        
        confirmation = new JScrollPane(textArea);
        confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        conPanel.add(confirmation, BorderLayout.CENTER);
		
        add(conPanel);
	}
	
	public class addStored implements ActionListener{
		String name, upc;
		int stock;
		
		public void actionPerformed(ActionEvent arg0) {
			name =nameText.getText();
			upc=upcText.getText();
			stock=Integer.valueOf(stockText.getText());
			try{
				manager.addStored(name, upc, stock);
				textArea.append("******************* Item Stored ******************" + "\n" +
						"\n" + "name: " + name +
						"\n" + "upc: " + upc +
						"\n" + "Stock: " + stock +
						"\n\n" +
						"*********************************************************" + "\n\n");
			}catch (SQLException e){
				textArea.append("Item already exists or does not exists.  Cannot be added!");
				e.printStackTrace();
			}
			
		}
	}
	
	public class removeItem implements ActionListener{
		String upc;
		String name;
		public void actionPerformed(ActionEvent arg0) {
			name=nameText.getText();
			upc=upcText.getText();
			try {
				manager.removeStoredItem(name, upc);
				textArea.append("******************* Item Deleted ******************" + "\n" +
						"\n" + "Supplier with upc: '" + upc + "' has been removed successfully" +
						"\n\n" +
						"*********************************************************" + "\n\n");
			} catch (SQLException e) {
				textArea.append("Item does not exist.  Cannot be deleted!");
				e.printStackTrace();
			}
			
		}
		
	}
}
